package util.ui;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.net.URL;

import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;


public class TCWEditorPane extends javax.swing.JEditorPane 
{
	public TCWEditorPane(URL linkURL) throws Exception 
	{
		super(linkURL);
		setEditable(false);
        Font font = UIManager.getFont("Label.font");
        String bodyRule = "body { font-family: " + font.getFamily() + "; " +
                "font-size: " + font.getSize() + "pt; }";
        ((HTMLDocument)getDocument()).getStyleSheet().addRule(bodyRule);

		//HTMLEditorKit editorKit = 
		//setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
		
		addHyperlinkListener(new HyperlinkListener() 
		{
		    public void hyperlinkUpdate(HyperlinkEvent e) 
		    {
		        if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
		        {
		        	
	        		try
	        		{
	        			String ustr = e.getDescription();
			        	//System.err.println("link:" + ustr);
	        			if (ustr.startsWith("http://"))
	        			{
				        	if(Desktop.isDesktopSupported()) 
				        	{
				        		Desktop.getDesktop().browse(e.getURL().toURI());
				        	}		        				
	        			}
	        			else
	        			{
	        				URL url = TCWEditorPane.class.getResource(ustr);
	        				setPage(url);
	        			}

	        		}
	        		catch(Exception eee)
	        		{
	        			System.err.println("Can't open link");
	        			System.err.println(eee.getMessage());
	        		}
		        }
		    }
		});
	}

	protected void paintComponent(Graphics g) 
	{
		Graphics2D g2 = (Graphics2D)g;

		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
		RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		super.paintComponent(g2);
	}
}